#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Specialized;
using System.Configuration;

using AirWeb.ApplicationTools.Common;
using AirWeb.DirectoryMgr;
using AirWeb.ApplicationTools.Image;
using AirWeb.ApplicationTools;


namespace FlashBack.Web
{
	/// <summary>
	/// Summary description for FilmStrip.
	/// </summary>
	public partial class AttributeEditor : ThumbNails
	{

		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		/// <summary>
		/// Displays an error message in the middle of the screen
		/// </summary>
		/// <param name="i_msg"></param>
		override protected void DisplayErrorMsg(string i_msg)
		{
			MsgLabel.Visible = true;
			MsgLabel.Text = i_msg;
		}

		override protected void SetTitle(string i_newTitle)
		{
			detailstitle.Text = i_newTitle;
		}

		override protected void SetNavigation(string i_newTitle, string i_newURL)
		{
			detailstitle.Text = i_newTitle;
		}

		override protected void Page_Load(object sender, EventArgs e)
		{
			base.Page_Load(sender,e);

			if(!IsPostBack)
			{
				NameValueCollection config = (NameValueCollection)ConfigurationSettings.GetConfig("flashBack/savedSearches");			
				DataTable dt = new DataTable();
				dt.Columns.Add(new DataColumn("Name", typeof(string)));
				dt.Columns.Add(new DataColumn("Query", typeof(string)));

				foreach(Object p in config)
				{
					DataRow dr = dt.NewRow();
					dr[0] = p;
					dr[1] = (string)config[(string)p];
					dt.Rows.Add(dr);
				}
			
				keywordList.DataSource = dt;
				keywordList.DataBind();
			}
		}

		override protected DirectoryHelper.FolderType GetFolderType()
		{
			return DirectoryHelper.FolderType.org;
		}

		override protected void BuildAndBind()
		{
			DataTable dt = new DataTable();
			DataRow dr;

			dt.Columns.Add(new DataColumn("tnpath", typeof(string)));
			dt.Columns.Add(new DataColumn("Title", typeof(string)));
			dt.Columns.Add(new DataColumn("Keywords", typeof(string)));
			dt.Columns.Add(new DataColumn("DateTaken", typeof(DateTime)));

			System.Collections.IEnumerator myEnumerator = m_contentList.GetEnumerator();

			while ( myEnumerator.MoveNext() )
			{
				dr = dt.NewRow();

				FileInfo f = (FileInfo)myEnumerator.Current;
				
				string ogvp = m_dirHelper.ReverseMapPath(f.FullName,m_dirHelper.OriginalsVirtualRoot);
				
				dr[0] = m_dirHelper.OG2TN(ogvp);
				dr[1] = f.FullName;
				dr[2] = "N/A";
				dr[3] = f.CreationTime;
					
				dt.Rows.Add(dr);
			}

			DataView v = new DataView(dt);
			v.Sort = "DateTaken DESC";
			DetailsGrid.DataSource = v;
			DetailsGrid.DataBind();
		}

		protected new void onRowBound(object sender, DataGridItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				try
				{
					PictureProperties p = new PictureProperties(e.Item.Cells[2].Text);

					ImageProperty ip = p.GetProperty(ImageProperty.ImagePropsIds.TITLE);
					e.Item.Cells[2].Text = (string) ip.Value;

					ip = p.GetProperty(ImageProperty.ImagePropsIds.KEYWORDS);
					e.Item.Cells[3].Text = (string) ip.Value;

					ip = p.GetProperty(ImageProperty.ImagePropsIds.DATE_PIC_TAKEN);
					e.Item.Cells[4].Text = (string) ip.Value;
				}
				catch
				{
				}
			}
		}

		protected new void ChangePageIndex(object Sender, DataGridPageChangedEventArgs e)
		{
			DetailsGrid.CurrentPageIndex = e.NewPageIndex;
			BuildAndBind();
		}

		protected void OnApply(object Sender, EventArgs e)
		{
			ApplyFileKeywords(true);
		}

		protected void OnClear(object Sender, EventArgs e)
		{
			ApplyFileKeywords(false);
		}

		void ApplyFileKeywords(bool i_set)
		{
			ArrayList selKeywords = new ArrayList();

			//Get the selected keywords
			foreach(DataListItem l in keywordList.Items)
			{
				System.Web.UI.WebControls.CheckBox kcb = (System.Web.UI.WebControls.CheckBox)l.FindControl("keyword");
				if(kcb != null && kcb.Checked)
				{
					selKeywords.Add(kcb.Text);
				}
			}

			if(selKeywords.Count == 0)
				return; // nothing to do

			//Get the selected pictures
			foreach(DataGridItem d in DetailsGrid.Items)
			{
				System.Web.UI.WebControls.CheckBox cb = (System.Web.UI.WebControls.CheckBox)d.FindControl("Select");
				if(cb != null && cb.Checked)
				{
					/*FileInfo f = (FileInfo)m_contentList[d.DataSetIndex];
					if(f != null)
					{
						PictureProperties p = new PictureProperties(f.FullName,true);

						if(i_set)
							p.ApplyKeywords(selKeywords);
						else
							p.ClearKeywords(selKeywords);

						//p.Save();
					}*/
				}
			}
			BuildAndBind();
		}
	}
}
